<?php

namespace App\Admin\Controllers;

use App\Models\Banner;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use \Log;
use \Cache;

class BannerController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('广告位管理');
            $content->description('广告位列表');

            $content->body($this->grid());
        });
    }

    /**
     * Show interface.
     *
     * @param mixed   $id
     * @param Content $content
     * @return Content
     */
    public function show($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('广告位管理');
            $content->description('编辑广告位');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('广告位管理');
            $content->description('编辑广告位');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('广告位管理');
            $content->description('添加广告位');
//            $content->withInfo('123' , '123');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Banner::class, function (Grid $grid) {
            $grid->id('广告ID')->sortable();
            $grid->name('广告名称');
            $grid->shops('广告商家');
            $grid->position('位置');
            $grid->img('广告图片')->image(env('BASE_URL'),100,100);

            $grid->type('位置')->display(function ($released) {
                $res = true;
                if ($released == 1) {
                    $res = '轮播图';
                }
//                elseif ($released == 1) {
//                    $res = '展示';
//                }
                return $res;
            })->sortable();
            $grid->sort('排序(序号小的在前)')->sortable();
            $grid->inttime('开始时间');
            $grid->endtime('结束时间');

            $grid->filter(function ($filter) {
                // 在这里添加字段过滤器
                $filter->equal('id', '广告ID');
                $filter->equal('type', '位置')->radio([
                    1 => '轮播图'
                ]);
                $filter->between('inttime', '开始时间')->datetime();
                $filter->between('endtime', '结束时间')->datetime();

            });

        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Banner::class, function (Form $form) {

            $form->display('id', '广告ID');
            $form->text('name','广告名称');
            $form->text('shops','广告商家');
            $form->text('position','位置');
            $form->image('img','广告图片')->uniqueName();
            $form->image('imgx','全面屏广告图片')->uniqueName();
            $form->multipleSelect('type','广告位')->options([
                1 => '轮播图'
            ]);
            $form->radio('turn_type', '跳转方式')->options([
                0 => 'webview',
                99 => '功能建设中',
            ]);
            $form->text('value','参数值(对应turn_type,不同的有不同的参数值)');
            $form->radio('tag', 'webview拼接标签')->options([
                'phone' => 'phone',
                'uid' => 'uid',
                'api_token' => 'api_token',
            ]);
            $form->datetime('inttime','开始时间');
            $form->datetime('endtime','结束时间');
//            $form->display('clicknum', '点击次数');
            $form->number('sort','排序(序号小的在前)')->default(100);
            $form->display('created_at', '创建时间');
            $form->display('updated_at', '更新时间');

            $form->saved(function (Form $form) {

                $banner = Banner::where('name',$form->name)->orderBy('updated_at', 'desc')->first();
                $date = date("Y-m-d");
                    $key = 'home_' . $banner->position . $date;
                    Cache::delete($key);
//                }
            });
        });
    }
}
